// pages/weather/weather.js
//var app = getApp()
var util = require('../../untils/untils.js') 
Page({
  /**
   * 页面的初始数据
   */
  data: {
    date:'2018-04-15',
    city:'广州',
    shidu:'97%',
    pm25:25,
    pm10:21,
    quality:'优',
    wendu:'16',
    forecast:[
      {
        date: "14日星期日",
        high: "高温 23.0℃",
        low: "低温 16.0℃",
        type: "中到大雨",
        notice: "带好雨具，别在树下躲雨"
      },
      {
        "date": "16日星期一",
        high: "高温 18.0℃",
        low: "低温 16.0℃",
        type: "中到大雨",
        notice: "出门带雨具，安全最重要"
      },
      {
        date: "17日星期二",
        high: "高温 23.0℃",
        low: "低温 17.0℃",
        type: "多云",
        notice: "阴晴之间，谨防紫外线侵扰"
      },
      {
        date: "18日星期三",
        high: "高温 25.0℃",
        low: "低温 18.0℃",
        type: "多云",
        notice: "阴晴之间，谨防紫外线侵扰"
      },
      {
        date: "19日星期四",
        high: "高温 25.0℃",
        low: "低温 19.0℃",
        type: "多云",
        notice: "阴晴之间，谨防紫外线侵扰"
      }
    ]
  },
  getWeather : function () {
    var that = this;
    util.fetch('https://www.sojson.com/open/api/weather/json.shtml?city=%E5%B9%BF%E5%B7%9E', function (data) {    
      if(data.status=='200'){
        let forecast = that.data.forecast;
        for (var i = 0; i < data.data.forecast.length; i++) {
          forecast[i].date = data.data.forecast[i].date;
          forecast[i].high = data.data.forecast[i].high;
          forecast[i].low = data.data.forecast[i].low;
          forecast[i].type = data.data.forecast[i].type;
          forecast[i].notice = data.data.forecast[i].notice;
        }
        that.setData({
          date : data.date,
          city : data.city,
          shidu: data.data.shidu,
          pm25: data.data.pm25,
          pm10: data.data.pm10,
          quality: data.data.quality,
          wendu: data.data.wendu,
          forecast: forecast
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getWeather();
 },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})